import getProp from '@/components/Form/fieldType/props/prop';

import { validateRequired, validateType } from '@/components/Form/fieldType/rules/utils';
import { validateStrLen } from '@/components/Form/fieldType/rules/string';
import { validateDecimal } from '@/components/Form/fieldType/rules/number';

const TIPS_INPUT = '输入';

/**
 * 获取验证字段
 * @param {*} { fields }
 * @return {*} rule
 */
function _getRules({ fields }) {
  const { label, required } = getProp({ fields });
  const { verification, setting } = fields;
  const { decimal_length = 0 } = setting;
  const { type, max_length, min_length } = verification;

  return {
    rules: [
      validateRequired(type, required, `请${TIPS_INPUT}${label}！`),
      validateType(type, TIPS_INPUT),
      validateStrLen({ max_length, min_length }, TIPS_INPUT),
      validateDecimal({ decimal_length: decimal_length || 0, message: '输入' })
    ]
  };
}

/**
 * @export 获取input类型表单属性
 * @param {*} { fields }
 * @return {*} prop
 */
export default function getInputProp({ fields }) {
  const rules = _getRules({ fields });

  const prop = getProp({ fields });

  return {
    ...prop,
    ...rules
  };
}
